package org.example.utils;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;

/**
 * @author 李国树
 * @version 1.0
 * @Desc
 * @date 2023/12/1 11:29
 */
public class IdcardUtils {
    public static void main(String[] args) throws IOException {
        boolean b = checkIdCard("李国树", "51132319860311337X");
        System.out.println(b);
    }
    public static boolean checkIdCard(String name, String idNo) throws IOException {
        String appcode="b30ebc64e4204432afe83d478e86b786";
        String url = "https://idenauthen.market.alicloudapi.com/idenAuthentication";
        String result = "";
        RequestBody formBody = new FormBody.Builder().add("name", name).add("idNo", idNo).build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appcode).post(formBody).build();

        Call call = new OkHttpClient().newCall(request);
        Response response = null;
        try {
            response = call.execute();
        } catch (IOException e) {
            System.out.println("execute failed, message:" + e.getMessage());
        }

        assert response != null;
        if (!response.isSuccessful()) {
            // 状态码为403时一般是套餐包用尽，需续购；注意：续购不会改变秘钥（appCode），仅增加次数
            // 续购链接：https://market.aliyun.com/products/57000002/cmapi025518.html
            System.out.println("request failed----" + "返回状态码" + response.code()  + ",message:" + response.message());
        }
        result = response.body().string();    //此处不可以使用toString()方法，该方法已过期
        if(result.contains("\"respCode\":\"0000\"")){
            return true;
        }else{
            return false;
        }
    }
}

 
